﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using LinqtoCRM.Tests.CRM;
using System.Reflection;

namespace LinqtoCRM.Tests
{
    class DummyService : ICrmService
    {
        private readonly string dummyResults;
        private static readonly string countResults = "<resultset morerecords=\"0\"><result><count>2</count></result></resultset>";

        private string read(string file)
        {
            Assembly a = Assembly.GetExecutingAssembly();
            string name = "LinqtoCRM.Tests." + file;
            string result;
            using (StreamReader s = new StreamReader(a.GetManifestResourceStream(name)))
            {
                result = s.ReadToEnd();
            }
            return result;
        }

        public DummyService()
        {
            dummyResults = read("DummyContacts.xml");
        }

        public DummyService(string file)
        {
            dummyResults = read(file);
        }

        public string Fetch(string fetchXml)
        {
            return fetchXml.Contains("aggregate") ? countResults : dummyResults;
        }

        public System.Net.ICredentials Credentials
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Url
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }       
    }
}
